package us.wili.dev.common.config;

import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

@Configuration
@ConfigurationProperties(prefix = "qcloudsms", ignoreUnknownFields = false)
public class QcloudSmsConfig {

    private static final Logger logger = LoggerFactory.getLogger(QcloudSmsConfig.class);
    private Integer appid;
    private String appkey;
    private String smsSign;
    private Integer loginBody;
    private Integer updatePwdBody;
    private Integer bindingBody;
    private Integer unbundleBody;
    private Integer consultBody;
    private Integer registerBody;

    /**
     * 发送登录验证码
     * @param phone 手机号
     * @param code 验证码
     * @param expiredAt 有效时长 （分钟）
     * @return
     */
    public boolean loginCodeSms(String phone,String code,int expiredAt){
       String[] params = new String[]{code,expiredAt+""};
       return aloneSms(phone,params,loginBody);
    }

    /**
     * 发送注册验证码
     * @param phone 手机号
     * @param code 验证码
     * @param expiredAt 有效时长 （分钟）
     * @return
     */
    public boolean registerSms(String phone,String code,int expiredAt){
        String[] params = new String[]{code,expiredAt+""};
        return aloneSms(phone,params,registerBody);
    }

    /**
     * 修改密码验证码
     * @param phone 手机号
     * @param code 验证码
     * @param expiredAt 有效时长 （分钟）
     * @return
     */
    public boolean updatePwdSms(String phone,String code,int expiredAt){
        String[] params = new String[]{code,expiredAt+""};
        return aloneSms(phone,params,updatePwdBody);
    }

    /**
     * 绑定手机验证码
     * @param phone 手机号
     * @param code 验证码
     * @param expiredAt 有效时长 （分钟）
     * @return
     */
    public boolean bindingSms(String phone,String code,int expiredAt){
        String[] params = new String[]{code,expiredAt+""};
        return aloneSms(phone,params,bindingBody);
    }
    /**
     * 解绑手机验证码
     * @param phone 手机号
     * @param code 验证码
     * @param expiredAt 有效时长 （分钟）
     * @return
     */
    public boolean unbundleSms(String phone,String code,int expiredAt){
        String[] params = new String[]{code,expiredAt+""};
        return aloneSms(phone,params,unbundleBody);
    }
    /**
     * 通知通知
     * @param phone 手机号
     * @return
     */
    public boolean consultSms(String phone){
        String[] params = new String[]{};
        return aloneSms(phone,params,consultBody);
    }


    /**
     * 单发模板短信
     * @param phone 手机号
     * @param params 替换信息
     * @param body 模板id
     * @return
     */
    public boolean aloneSms(String phone,String[] params,int body){
        SmsSingleSender ssender = new SmsSingleSender(appid, appkey);
        try {
            SmsSingleSenderResult result = ssender.sendWithParam("86", phone,
                    body, params, smsSign, "", "");
            if (result.result==0){
                return true;
            }else {
                logger.info("短信发送失败，手机号："+phone+",错误码："+result.result+",错误信息"+result.errMsg);
                return false;
            }
        } catch (HTTPException e) {
            e.printStackTrace();
            logger.info("短信发送失败，手机号："+phone+","+",错误信息"+e.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
            logger.info("短信发送失败，手机号："+phone+","+",错误信息"+e.getMessage());
        }
        return false;
    }




    public Integer getAppid() {
        return appid;
    }

    public void setAppid(Integer appid) {
        this.appid = appid;
    }

    public String getAppkey() {
        return appkey;
    }

    public void setAppkey(String appkey) {
        this.appkey = appkey;
    }

    public String getSmsSign() {
        return smsSign;
    }

    public void setSmsSign(String smsSign) {
        this.smsSign = smsSign;
    }

    public Integer getLoginBody() {
        return loginBody;
    }

    public void setLoginBody(Integer loginBody) {
        this.loginBody = loginBody;
    }

    public Integer getUpdatePwdBody() {
        return updatePwdBody;
    }

    public void setUpdatePwdBody(Integer updatePwdBody) {
        this.updatePwdBody = updatePwdBody;
    }

    public Integer getBindingBody() {
        return bindingBody;
    }

    public void setBindingBody(Integer bindingBody) {
        this.bindingBody = bindingBody;
    }

    public Integer getUnbundleBody() {
        return unbundleBody;
    }

    public void setUnbundleBody(Integer unbundleBody) {
        this.unbundleBody = unbundleBody;
    }

    public Integer getConsultBody() {
        return consultBody;
    }

    public void setConsultBody(Integer consultBody) {
        this.consultBody = consultBody;
    }

    public Integer getRegisterBody() {
        return registerBody;
    }

    public void setRegisterBody(Integer registerBody) {
        this.registerBody = registerBody;
    }
}
